package com.github.wxiaoqi.security.biz.modules.merchant.biz;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.wxiaoqi.security.biz.common.biz.request.QueryApkVersionReq;
import com.github.wxiaoqi.security.biz.common.crm.request.QueryApkVersionListRequest;
import com.github.wxiaoqi.security.biz.common.msg.BizResponseCode;
import com.github.wxiaoqi.security.biz.modules.merchant.entity.ApkVersion;
import com.github.wxiaoqi.security.biz.modules.merchant.mapper.ApkVersionMapper;
import com.github.wxiaoqi.security.common.biz.BaseBiz;
import com.github.wxiaoqi.security.common.msg.ResponseCode;
import com.github.wxiaoqi.security.common.msg.TableResultResponse;
import com.github.wxiaoqi.security.common.util.EntityUtils;
import com.github.wxiaoqi.security.common.util.Query;

import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/**
 * APK版本信息
 *
 * @author yyn
 * @email ${email}
 * @date 2018-07-16 10:15:49
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class ApkVersiontBiz extends BaseBiz<ApkVersionMapper, ApkVersion> {
	@Autowired
	private ApkVersionMapper apkVersionMapper;
	/**
	 * 查询商户基础信息列表
	 * @author yyn
	 * @time 创建时间:2019年3月6日下午3:31:36
	 * @param queryApkVersionListRequest
	 * @return
	 */
	public Map<String, Object> queryList(QueryApkVersionListRequest queryApkVersionListRequest){
		log.info("【查询apk版本信息列表】   请求参数：{}",EntityUtils.beanToMap(queryApkVersionListRequest));
		Map<String, Object> respMap = new HashMap<String, Object>();
		String version = queryApkVersionListRequest.getVersion();
		String icon = queryApkVersionListRequest.getIcon();
		String isAppPos = queryApkVersionListRequest.getIsAppPos();
		String apkUrl = queryApkVersionListRequest.getApkUrl();
		String content = queryApkVersionListRequest.getContent();
		String isCompelUpd = queryApkVersionListRequest.getIsCompelUpd();
		Date createDate = queryApkVersionListRequest.getCreateDate();
		
		int page = queryApkVersionListRequest.getPage();
		int limit = queryApkVersionListRequest.getLimit();
		Map<String, Object> params = new HashMap<String, Object>();
	    params.put("page", page);
	    params.put("limit", limit);
	    Query query = new Query(params);
	    Page<Object> result = PageHelper.startPage(query.getPage(), query.getLimit());
		
		Example example = new Example(ApkVersion.class);
        Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(version)) {
        	if(criteria==null){
        		criteria=example.createCriteria();
        	}
        	criteria.andEqualTo("version", version);
		}
        if (!StringUtils.isEmpty(icon)) {
        	if(criteria==null){
        		criteria=example.createCriteria();
        	}
        	criteria.andEqualTo("icon", icon);
		}
        if (!StringUtils.isEmpty(isCompelUpd)) {
        	if(criteria==null){
        		criteria=example.createCriteria();
        	}
        	criteria.andEqualTo("isCompelUpd", isCompelUpd);
		}
        if (!StringUtils.isEmpty(content)) {
        	if(criteria==null){
        		criteria=example.createCriteria();
        	}
        	criteria.andEqualTo("content", content);
		}
        if (!StringUtils.isEmpty(isAppPos)) {
        	if(criteria==null){
        		criteria=example.createCriteria();
        	}
        	criteria.andEqualTo("isAppPos", isAppPos);
		}
        if (!StringUtils.isEmpty(apkUrl)) {
        	if(criteria==null){
        		criteria=example.createCriteria();
        	}
        	criteria.andEqualTo("apkUrl", apkUrl);
        }
		example.setOrderByClause("create_date desc");
        List<ApkVersion> list = mapper.selectByExample(example);
        
        TableResultResponse<ApkVersion> table = new TableResultResponse<ApkVersion>(result.getTotal(), list);
        respMap.put("body", table);
        respMap.put("code", BizResponseCode.OK.getCode());
        respMap.put("message", BizResponseCode.OK.getMessage());
		log.info("【查询apk版本信息列表】   响应参数：{}",respMap);
		return respMap;
	}
	
	/**
	 * 添加APK版本信息
	 * @author yyn
	 * @time 创建时间:2018年8月24日下午2:50:55
	 * @param queryApkVersionListRequest
	 * @return
	 */
	public Map<String, Object> add(QueryApkVersionListRequest queryApkVersionListRequest){
		log.info("【添加添加APK版本信息】   请求参数：{}",EntityUtils.beanToMap(queryApkVersionListRequest));
		Map<String, Object> respMap = new HashMap<String, Object>();
		ApkVersion apkVersion=new ApkVersion();
		BeanUtils.copyProperties(queryApkVersionListRequest,apkVersion);
		apkVersion.setCreateDate(new Timestamp(System.currentTimeMillis()));
		apkVersionMapper.insert(apkVersion);
		
		respMap.put("code", BizResponseCode.OK.getCode());
        respMap.put("msg", BizResponseCode.OK.getMessage());
		log.info("【添加添加APK版本信息】   响应参数：{}",respMap);
		return respMap;
	}
	
	/**
	 * 根据序号删除商户基础信息
	 * @author yyn
	 * @time 创建时间:2018年8月24日下午2:50:55
	 * @param id
	 * @return
	 */
	public Map<String, Object> deleteOne(Integer id){
		Map<String, Object> respMap = new HashMap<String, Object>();
		log.info("【根据序号删除版本信息】   请求参数：id:{}",id);
		ApkVersion apkVersion=new ApkVersion();
		apkVersion.setSeq(id);
		apkVersionMapper.delete(apkVersion);
		respMap.put("code", BizResponseCode.OK.getCode());
	    respMap.put("message", BizResponseCode.OK.getMessage());
	    log.info("【根据序号删除版本信息】   响应参数：{}",respMap);
		return respMap;
	}
	
	
	/**
	 * 根据序号修版本信息
	 * @author yyn
	 * @time 创建时间:2018年8月24日下午2:50:55
	 * @param queryApkVersionListRequest
	 * @return
	 */
	public Map<String, Object> updateById(QueryApkVersionListRequest queryApkVersionListRequest){
		Map<String, Object> respMap = new HashMap<String, Object>();
		log.info("【根据序号修改版本信息】   请求参数：",EntityUtils.beanToMap(queryApkVersionListRequest));
		ApkVersion apkVersion = new ApkVersion();
		BeanUtils.copyProperties(queryApkVersionListRequest,apkVersion);
		apkVersion.setCreateDate(new Timestamp(System.currentTimeMillis()));
		try {
			int num = apkVersionMapper.updateByPrimaryKeySelective(apkVersion);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		respMap.put("code", BizResponseCode.OK.getCode());
	    respMap.put("message", BizResponseCode.OK.getMessage());
	    log.info("【根据序号修改版本信息】   响应参数：{}",respMap);
		return respMap;
	}
	
	/**
	 * 查询单个apk版本信息
	 * @param param
	 * @return
	 */
	public Map<String, Object> queryApkVersionInfo(QueryApkVersionReq params) {
		log.info("apk版本信息查询。。。{}"+params);
		Map<String, Object> map = new HashMap<String, Object>(); 
		String appIcon = params.getAppIcon();
		String isAppPos = params.getIsAppPos();
		ApkVersion apkVersion = new ApkVersion();
		apkVersion.setIcon(appIcon);
		String ter = "";
		if (isAppPos.equals("POS")) {
			ter = "03";
		}
		if (isAppPos.equals("Android")) {
			ter = "01";
		}
		if (isAppPos.equals("iOS")) {
			ter = "02";
		}
		
		apkVersion.setIsAppPos(ter);
		ApkVersion apk = apkVersionMapper.queryOne(apkVersion);
		if(null == apk){
			map.put("code", ResponseCode.FUNCTION_NO_EXIST.getCode());
			map.put("msg", ResponseCode.FUNCTION_NO_EXIST.getMessage());
			return map;
		}
		String apkUrl = apk.getApkUrl();
		String content = apk.getContent();
		String version = apk.getVersion();
		String isCompelUpd = apk.getIsCompelUpd();
		map.put("code", ResponseCode.OK.getCode());
		map.put("msg", ResponseCode.OK.getMessage());
		map.put("apkUrl", apkUrl);
		map.put("content", content);
		map.put("appVersion", version);
		map.put("isCompelUpd", isCompelUpd);
		log.info("apk版本信息查询结束。。。。。{}",map);
		return map;
	}
	
	
	
}